package com.example.lots_of_demo.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.lots_of_demo.dao.*;
import com.example.lots_of_demo.model.*;
import com.example.lots_of_demo.utils.DateUtils;
import com.example.lots_of_demo.utils.R;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/11
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private RUserRoleDao rUserRoleDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> stringQueryWrapper = new LambdaQueryWrapper<>();
        stringQueryWrapper.eq(User::getUsername,username);
        User user = userDao.selectOne(stringQueryWrapper);
        if(user != null){
            MyPasswordEncoder encoder = MyPasswordEncoder.getInstance();
            encoder.setSalt(user.getSalt());
            Set<String> permissions = userDao.selectUserPermissions(user.getId());
            return new SecurityUser(user.getId(), username, user.getPassword(), user.getEnable(), permissions);
        }
        throw new UsernameNotFoundException("用户名或密码错误!");
    }

    @Transactional
    public R register(User user) {
        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(User::getUsername, user.getUsername());
        User checkUser = userDao.selectOne(userQueryWrapper);
        if(checkUser != null){
            return R.createFail(REnum.RFailResponse.USERNAME_IS_EXIST);
        }
        String salt = UUID.randomUUID().toString().substring(0,8);
        String encryptedPwd = Hashing.md5().newHasher().putString(user.getPassword() + salt, Charsets.UTF_8).hash().toString();
        user.setSalt(salt).setPassword(encryptedPwd).setEnable(UserEnum.UserEnable.IS_ENABLE.getCode());
        //插入用户
        userDao.insert(user);
        //插入普通用户的角色
        LambdaQueryWrapper<Role> roleQueryWrapper = new LambdaQueryWrapper<>();
        roleQueryWrapper.eq(Role::getName, RoleEnum.RoleName.COMMON_USER.getCode());
        Role role = roleDao.selectOne(roleQueryWrapper);
        RUserRole rUserRole = new RUserRole();
        rUserRole.setUserId(user.getId()).setRoleId(role.getId()).setCreateDateTime(DateUtils.getCurrentDateTime());
        rUserRoleDao.insert(rUserRole);
        return R.createSuccess(REnum.RSuccessResponse.REGISTER_SUCCESS_RESPONSE);
    }
}
